/** SampleWindow
 * 
 * SampleWindow stores in a table a fixed number of sampled values.
 * When the table is full the table_full signal is issued,
 * giving the average of the values stored in the table at that time.
 * 
*/

#include "data.h"

/** SampleWindow parameters, default values */
#ifndef SAMPLEWINDOW_SIZE
#define SAMPLEWINDOW_SIZE 5
#endif

module SampleWindowP{
	provides interface SampleWindow;
}

implementation {

	DataEntry table[SAMPLEWINDOW_SIZE];
	uint8_t entry_index;
	DataEntry mean;
	
	/** Add entry to the table. */
	command void SampleWindow.store(DataEntry *entry) {
		table[entry_index] = *entry;
		entry_index = (entry_index + 1) % SAMPLEWINDOW_SIZE;
		
		if (entry_index == 0) {
			fill_mean(&mean, table, SAMPLEWINDOW_SIZE);
			signal SampleWindow.table_full(&mean);
		}
	}
	
	command DataEntry* SampleWindow.get_mean() {
		return &mean;
	}
	
}